// pages/orderlaji/index.js
let App = getApp(),
  wxParse = require("../../wxParse/wxParse.js");

var util = require('../../utils/util.js');



var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    address_id:0,
  },
  radioChange: function (e) {

    var items = this.data.timelist;
    for (var i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value == e.detail.value
    }

    this.setData({
      timelist: items,
      book_time: e.detail.value
    });
  },



  bindDateChange: function (e) {

    var date = util.formatDate(new Date(this.data.datelist[e.detail.value]));

    if (date == util.formatDate(new Date())) {
      var timelist = util.formatTimeList(new Date(), 0);

    } else {
      var timelist = util.formatTimeList(new Date(), 1);

    }
    this.setData({
      date: this.data.datelist[e.detail.value],
      timelist: timelist,
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: this.data.timelist[e.detail.value]
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var date = util.formatDate(new Date());
    var datelist = util.formatDatelist(new Date());
    var timelist = util.formatTimeList(new Date(), 0);
    qqmapsdk = new QQMapWX({
      key: 'GEXBZ-GSKC4-7KRUW-X3GNK-H2WBE-ALFGN'
    });

    this.setData({
      date: date,
      timelist: timelist,
      datelist: datelist
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    var that = this
    wx.getLocation({
      success: function (res) {

        // 调用接口
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (res2) {
           
            that.setData({
              hasLocation: true,
              address_component: res2.result.address_component,
              ad_info: res2.result.ad_info.name,
              street: res2.result.address_component.street_number
            })
     
            that.getRegionList();
          },
          fail: function (re2) {
            // console.log(res2);
          },
          complete: function (res2) {
            console.log(res2);
          }
        });

      }
    })
  },

  /**
    * 获取当前订单信息
    */
  getAddressData: function () {
    let _this = this,
      options = _this.data.options;

    // 获取订单信息回调方法
    let callback = function (result) {
      if (result.code !== 1) {
        App.showError(result.msg);
        return false;
      }
      // 显示错误信息
      if (result.data.has_error) {
        _this.data.hasError = true;
        _this.data.error = result.data.error_msg;
        App.showError(_this.data.error);
      }
      _this.setData(result.data);
    };
 
    // App._get('Rorder/default_address', '', function (result) {
    //   callback(result);
    // });

    App._get('call/callNow', {
      'province': '浙江省',
      'city': '杭州市',
      'region': '滨江区'
    }, function (result) {

      console.log(result);
      callback(result);
    });

  },
  /**
   * 获取区域列表
   */
  getRegionList: function () {
    let _this = this;

    App._get('Region/getRegionList', {
      ad_info: _this.data.ad_info
    }, function (result) {
     
      // 初始化商品详情数据
      _this.setData({
        region: [_this.data.address_component.province,        _this.data.address_component.city, _this.data.address_component.district]
      })
    });
  },
  /**
   * 修改地区
   */
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  /**
 * 表单提交
 */
  saveData: function (e) {

    console.log(e.detail);
    let _this = this,
      values = e.detail.value
    values.region = this.data.region;

    // 表单验证
    if (!_this.validation(values)) {
      App.showError(_this.data.error);
      return false;
    }

    // 按钮禁用
    _this.setData({
      disabled: true
    });

    // 提交到后端
    App._post_form('Rorder/callOrder', values, function (result) {
      App.showSuccess(result.msg, function () {
        // wx.navigateBack();

        wx.makePhoneCall({
          phoneNumber: '13588045795',
          success: function () {
            console.log("成功拨打电话")
          }
        })

      });
    }, false, function () {
      // 解除禁用
      _this.setData({
        disabled: false
      });
    });
  },

  /**
   * 表单验证
   */
  validation: function (values) {
    if (values.name === '') {
      this.data.error = '收件人不能为空';
      return false;
    }
    if (values.phone.length < 1) {
      this.data.error = '手机号不能为空';
      return false;
    }
    if (values.phone.length !== 11) {
      this.data.error = '手机号长度有误';
      return false;
    }
    let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!reg.test(values.phone)) {
      this.data.error = '手机号不符合要求';
      return false;
    }
    if (!this.data.region) {
      this.data.error = '省市区不能空';
      return false;
    }
    if (values.detail === '') {
      this.data.error = '详细地址不能为空';
      return false;
    }
    if (values.detail === '') {
      this.data.error = '详细地址不能为空';
      return false;
    }
    return true;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
console.log("onshow");
    // 获取当前订单信息
    this.getAddressData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})